﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Utils.SmartMosaicServerWebservice;

namespace SSM.SmartMosaicUtils.Net
{
    /// <summary>
    /// 파일을 업로드하는 클래스
    /// </summary>
    public class FileUploader
    {
        /// <summary>
        /// 파일을 업로드하는 클래스
        /// </summary>
        /// <param name="fileName"></param>
        public UploadResult UploadFile(string filename, int themeCode)
        {
            try
            {
                // get the exact file name from the path
                String strFile = System.IO.Path.GetFileName(filename);

                // create an instance fo the web service
                Utils.SmartMosaicServerWebservice.SmartMosaicServerWebservice srv = 
                    new Utils.SmartMosaicServerWebservice.SmartMosaicServerWebservice();

                // get the file information form the selected file
                FileInfo fInfo = new FileInfo(filename);

                // get the length of the file to see if it is possible
                // to upload it (with the standard 4 MB limit)E
                long numBytes = fInfo.Length;
                double dLen = Convert.ToDouble(fInfo.Length / 1000000);

                // Default limit of 4 MB on web server
                // have to change the web.config to if
                // you want to allow larger uploads
                if (dLen < 4)
                {
                    // set up a file stream and binary reader for the 
                    // selected file
                    FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fStream);

                    // convert the file to a byte array
                    byte[] data = br.ReadBytes((int)numBytes);
                    br.Close();

                    // pass the byte array (file) and file name to the web service
                    UploadResult result = srv.UploadFile(data, strFile, themeCode);
                    fStream.Close();
                    fStream.Dispose();

                    //string filepath = "";
                    //string FileDir = filepath.Substring(0, filepath.Length - filename.Length);
                    //string FolderName = filename.Substring(0, filename.Length - 4);
                    //FileDir = FileDir + "DeepZoom/" + FolderName;
                    //string resultDir = FileDir + "/ExportImages";
                    //string resultXML = resultDir + "/" + FolderName + ".xml";

                    //Directory.CreateDirectory(FileDir);
                    //Directory.CreateDirectory(resultDir);

                    //ImageCreator creator = new ImageCreator();
                    //creator.TileSize = 256;
                    //creator.TileFormat = ImageFormat.Jpg;
                    //creator.CopyMetadata = false;
                    //creator.Create(filepath, resultXML);

                    if (result.UploadResultCode == UploadResultCodeEnum.Fail)
                        throw new NetException(result.Detail);

                    return result;
                }
                else
                {
                    // Display message if the file was too large to upload
                    throw new NetException("The file selected exceeds the size limit for uploads.");
                }
            }
            catch (Exception ex)
            {
                throw new NetException("It can't upload", ex);
            }
        }
    }
}
